
function $(id){
	return document.getElementById(id);
}

window.onload = function(){
	/*将光标定位在输入框*/
	$('user').focus();
	/*填写重置按钮*/
	$('reset').onclick = function(){
		$('user').value='';
		$('password').value='';
		$('password2').value=''; 
		$('email').value='';
		$('realname').value='';
		$('number').value='';
		$('question').value='';
		$('answer').value='';
		$('birth').value=''
	}
	/*设置注册激活开关*/
	var user1,user2,pswd1,pswd2,email1,email2;
	function checkreg(){
		if ((user1=='yes')&&(user2=='yes')&&(pswd1=='yes')&&(pswd2=='yes')&&(email1=='yes')&&(email2=='yes')) {
			$('reg').disabled=false;
		}else{
			$('reg').disabled=true;
		}
	}
	checkreg();
	/*检测用户名书写规范*/
	$('user').onkeyup=function(){
		name=$('user').value;
		user1='';
		if(name.match(/^[a-zA-Z_]*/)==''){
			$('userdiv').innerHTML='用户名只能用英文或下划线开头';
			$('userdiv').style.color='red';
			user1='';
		}else if(name.length<=2){
			$('userdiv').innerHTML='用户名必须要大于两位';
			$('userdiv').style.color='red';
			user1='';
		}else{
			$('userdiv').innerHTML='请输入用户名';
			$('userdiv').style.color='black';
			user1='yes';
		}
	}
	/*检测数据库中用户名是否已经存在*/
	$('user').onblur = function(){
		name = $('user').value;
		if(user1=='yes'){
			xmlhttp.open('get','actuion/checkName.php?action=1&user='+name,true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4){
					if(xmlhttp.status == 200){
						var msg = xmlhttp.responseText;
						if(msg =='1'){
							$('userdiv').innerHTML='恭喜你改用户名可以使用!';
							$('userdiv').style.color='green';
							user2='yes';
						}else if(msg =='2'){
							$('userdiv').innerHTML='该用户名已存在去请重新输入';
							$('userdiv').style.color='red';
							user2='';
						}else{
							$('userdiv').innerHTML=msg;
							$('userdiv').style.color='red';
							user2='';
						}
					}
				}
				checkreg();
			}
			xmlhttp.send(null);
		}
	}
	/*验证密码*/
	$('password').onkeyup=function(){
		pwd = $('password').value;
		pwd2 = $('password2').value;
		if( pwd.length < 6){
			$('pswdiv').innerHTML='密码长度必须大于6位';
			$('pswdiv').style.color='red';
		}else if(pwd.length >6 && pwd.length < 12){
			$('pswdiv').innerHTML='密码符合要求，密码强度：弱！';
			$('pswdiv').style.color='green';
			pswd1='yes';
		}else if(pwd.match(/^[0-9]*$/)!=null || pwd.match(/^[a-zA-Z]*$/) != null){
			$('pswdiv').innerHTML='密码符合要求，密码强度：中';
			$('pswdiv').style.color='green';
			pswd1='yes';
		}else{
			$('pswdiv').innerHTML='密码符合要求，密码强度：强';
			$('pswdiv').style.color='green';
			pswd1='yes';
		}
		checkreg();
	}

	$('password2').onkeyup=function(){
		pwd=$('password').value;
		pwd2=$('password2').value;
		if(pwd!=pwd2){
			$('pswdiv2').innerHTML='两次输入密码不一致';
			$('pswdiv2').style.color='red';
			pswd2='';
		}else{
			$('pswdiv2').innerHTML='密码输入正确';
			$('pswdiv2').style.color='green';
			pswd2='yes';
		}
		checkreg();
	}
	/*检测邮箱格式*/
	$('email').onkeyup=function(){
		em=$('email').value;
		ematch=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		if(em.match(ematch)==null){
			$('emaildiv').innerHTML='输入的邮箱格式错误，请重新填写';
			$('emaildiv').style.color='red';
			email1='';
		}else {
			$('emaildiv').innerHTML='邮箱填写正确';
			$('emaildiv').style.color='green';
			email1='yes';
		}
		checkreg();
    }
    /*检测邮箱是否已经被注册*/
	$('email').onblur=function () {
		email_1=$('email').value;
		if(email1=='yes'){
		xmlhttp.open('get','actuion/checkName.php?action=2&email='+email_1,true);
		xmlhttp.onreadystatechange=function () {
			if(xmlhttp.readyState==4){
				if(xmlhttp.status==200){
					var msg1 = xmlhttp.responseText;
					if(msg1=='1'){
						$('emaildiv').innerHTML='该邮箱可以使用';
						$('emaildiv').style.color='green';
						email2='yes';
					}else if(msg1=='2'){
						$('emaildiv').innerHTML='该邮箱已经被占用';
						$('emaildiv').style.color='red';
						email2='';
					}else {
						$('emaildiv').innerHTML=msg1;
						$('emaildiv').style.color='red';
						email2='';
					}
				}
			}
            checkreg();
		}
		xmlhttp.send(null);
		}

    }
	/*控制详细信息显示隐藏*/
	$('morbtn').onclick = function(){
		if($('morediv').style.display=='none'){
			$('morediv').style.display='block';
		}else{
			$('morediv').style.display='none';
		}
	}
	/*提交注册*/
	$('reg').onclick = function () {
		 var url='actuion/check_reg.php?usrName='+$('user').value+'&psw='+$('password').value+'&email='+$('email').value;
		 url+='&realName='+$('realname').value+'&tel='+$('number').value;
		 url+='&question='+$('question').value+'&answer='+$('answer').value+'&birth='+$('birth').value;
		 xmlhttp.open('get',url,true);
		 xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
		 xmlhttp.onreadystatechange=function(){
		 		if (xmlhttp.readyState==4) {
		 			if (xmlhttp.status==200) {
		 				msg= xmlhttp.responseText;
		 				if(msg=='1'){
		 					alert('账号注册成功，请到邮箱激活');
		 					//locathion='main.php';
		 				}else if (msg=='-1') {
		 					alert('注册失败！');
		 					exit();
		 				}else{
		 					alert(msg);
		 					exit();
		 				}
		 			}
		 		}
		 }
		 xmlhttp.send(null);
    }
}